package com.suncaper.han_yi_system.controller;

import com.suncaper.han_yi_system.pojo.entity.Registration;
import com.suncaper.han_yi_system.pojo.vo.RegistrationVo;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.RegistrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/registration")
@CrossOrigin(origins = "*")
@Tag(name = "登记管理")
@Slf4j
public class RegistrationController {

    @Autowired
    RegistrationService registrationService;


    /*；
     * 增加时间判断
     * */
    @Operation(summary = "新增登记信息")
    @PostMapping("/save")
    public Result save(@RequestBody Registration registration){

        registration.setCreateTime(LocalDateTime.now());
        registrationService.saveRegistration(registration);
        return Result.ok();
    }

    @Operation(summary = "更新登记信息")
    @PutMapping("/update")
    public Result update(@RequestBody Registration registration){
        registration.setUpdateTime(LocalDateTime.now());
        registrationService.updateById(registration);
        return Result.ok();
    }

    @Operation(summary = "学生登记信息修改")
    @PostMapping("/updateByStudent")
    public Result updateStudentInfo(@RequestBody Registration registration){
        int i = registrationService.updateByStudent(registration);
        if(i == 0) return Result.error().message("登记时间已截止，无法修改信息");
        return Result.ok();
    }

    @Operation(summary = "删除登记信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        registrationService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取登记信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        if(id == null){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("registration", registrationService.getById(id));
    }

    @Operation(summary = "获取登记列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("registrationList", registrationService.list());
    }

    @Operation(summary = "获取我的登记列表")
    @GetMapping("/myRegistration")
    public Result getMyRegistration(){
        List<RegistrationVo> registrationVoList = registrationService.findByUserId();
        return Result.ok().data("registrationVoList", registrationVoList);
    }

}